var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
    data: {
        id: 0,// 当前选中分类
        goodsList: [],
        categoryList: [],
        currentCategory: {},
        scrollLeft: 0,
        scrollTop: 0,
        goodsCount: 0,
        scrollHeight: 0,
        page: 1,
        size: 10,
        totalPages: 1
    },
    onLoad: function(options) {
        //初始化页面信息
        this.pageInit(options);
        this.getCatalog();
    },
    onPullDownRefresh() {
        wx.showNavigationBarLoading();//在标题栏中显示加载
        this.getGoodsList();
        wx.hideNavigationBarLoading();//完成停止加载
        wx.stopPullDownRefresh();//停止下拉刷新
    },
    pageInit(options) {
        // 页面初始化 options为页面跳转所带来的参数
        var that = this;
        if (options.id) {
            that.setData({
                id: parseInt(options.id)
            });
        }

        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });

        util.request(api.GoodsCount).then(function(res) {
            that.setData({
                goodsCount: res.data.goodsCount
            });
        });
    },
    getCatalog: function() {
        //CatalogList
        let that = this;
        wx.showLoading({
            title: '加载中...',
        });
        util.request(api.CateList).then(function(res) {
            that.setData({
                categoryList: res.data.categoryList,
                currentCategory: res.data.currentCategory,
                id: res.data.currentCategory.id
            });
            that.getGoodsList();
            wx.hideLoading();
        });
    },
    getGoodsList() {
        var that = this;

        util.request(api.GoodsList, {
            categoryId: that.data.id,
            page: that.data.page,
            size: that.data.size
        })
            .then(function(res) {
                that.setData({
                    goodsList: that.data.goodsList.concat(res.data.goodsList),
                    totalPages: res.data.totalPages
                });
            });
    },
    getCurrentCategory: function(id) {
        let that = this;
        let current = that.data.categoryList.find(function (obj, index, arr) {
            return id == obj.id;
        });
        if (current === undefined) {
            // 提示失败
            return false;
        }else{
            that.setData({
                currentCategory: current,
            });
            that.getGoodsList();
        }
    },
    onReady: function() {
        // 页面渲染完成
    },
    onShow: function() {
        // 页面显示
    },
    onHide: function() {
        // 页面隐藏
    },
    onUnload: function() {
        // 页面关闭
    },
    switchCate: function(event) {
        var that = this;
        var currentTarget = event.currentTarget;
        if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
            return false;
        }
        this.setData({
            id: event.currentTarget.dataset.id,
            currentCategory: {},
            goodsList: [],
            page: 1,
            totalPages: 1
        });

        this.getCurrentCategory(event.currentTarget.dataset.id);
    },
    onReachBottom:function() {
        if (this.data.totalPages > this.data.page) {
            this.setData({
                page: this.data.page + 1
            });
        } else {
            wx.showToast({
                title: '已经到底了!',
                icon: 'none',
                duration: 2000
            });
            return false;
        }
        this.getGoodsList();
    }
})